/* eslint-disable */
// window.localStorage.setItem;
// window.localStorage.getItem;
import vueBus from "./bus.js";
let [init, arrType, odata, data] = ["czySetNameczyczyczyczy", [], null, null]
let arr = [0, "", true, null, undefined, [], {}, Symbol, window, document, new Function(), new RegExp(), new Error(), new Date()]

let getType = obj => {
    let type = typeof obj;
    if (type != "object")
        return type;
    return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1').toLowerCase();
}

let setData = function (obj, flag) {
    if (getType(obj) == "object") {
        for (let key in obj) {
            if (key == init) {
                if (window.localStorage.getItem(getType(obj[key])) == undefined || flag)
                    window.localStorage.setItem(getType(obj[key]), JSON.stringify({}))
            } else {
                let val = JSON.parse(window.localStorage.getItem(getType(obj[key])))
                val[key] = obj[key];
                window.localStorage.setItem(getType(obj[key]), JSON.stringify(val))
                odata = new GetData();
                data = {
                    ...odata.init(),
                    clear: odata.clear
                }
                vueBus.$emit("setData", data);
            }
        }

    }
}
arr.forEach(v => {
    let obj = {};
    obj[init] = v;
    setData(obj);
    arrType.push(getType(v))
})
let myData = key => {
    let arr = [];
    let obj = {};
    arrType.forEach(v => {
        obj = JSON.parse(window.localStorage.getItem(v))
        obj[key] ? arr.push(obj[key]) : null
    })
    return arr
}
class GetData {
    constructor() {
        this.init()
    }
    init() {
        let vals = {};
        let obj = null;
        arrType.forEach(v => {
            obj = JSON.parse(window.localStorage.getItem(v))
            for (let key in obj) {
                vals[key] = obj[key]
            }
        })
        return vals;
    }
    clear() {
        arr.forEach(v => {
            let obj = {};
            obj[init] = v;
            setData(obj, true);
        })
        odata = new GetData();
        data = {
            ...odata.init(),
            clear: odata.clear
        }
        vueBus.$emit("setData", data);
    }
}
odata = new GetData();
data = {
    ...odata.init(),
    clear: odata.clear
}
vueBus.$emit("setData", data);
export { setData, myData, data };
/* eslint-disable */
